package com.example.x6.configurationmaintenance.Utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.example.x6.configurationmaintenance.R;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;

public class PowerKitDaemonUtil {
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private CopyFile copyFile;
    private final String daemonName = "daemon_powerkit";
    private final String daemonPath = "/system/xbin/daemon_powerkit";
    private final String isDaemonRuningKey = "isDaemonRuningKey";
    private final String pidInfo = "pidInfo";
    private SharedPreferences sharedPreferences;
    private SuCommand suCommand;
    private final String tempPath = "/sdcard/";

    public PowerKitDaemonUtil(Context context2) {
        this.context = context2;
        this.sharedPreferences = context2.getSharedPreferences("pidInfo", 0);
        this.suCommand = new SuCommand();
        this.copyFile = new CopyFile();
    }

    private boolean isDaemonExisted() {
        Log.d(this.TAG, "isDaemonExisted");
        if (new File("/system/xbin/daemon_powerkit").exists()) {
            return true;
        }
        return false;
    }

    private void initDaemon() {
        Log.d(this.TAG, "initDaemon");
        this.copyFile.copyFileFromRaw(this.context, R.raw.daemon_powerkit, "daemon_powerkit", "/sdcard/");
        this.suCommand.execRootCmdSilent("mount -o rw,remount /system");
        this.suCommand.execRootCmdSilent("cp /sdcard/daemon_powerkit /system/xbin/daemon_powerkit");
        this.suCommand.execRootCmdSilent("rm /sdcard/daemon_powerkit");
        this.suCommand.execRootCmdSilent("chmod 655 /system/xbin/daemon_powerkit");
        this.suCommand.execRootCmdSilent("mount -o r,remount /system");
        this.suCommand.execRootCmdSilent("sync");
    }

    private boolean isDaemonRunning() {
        if (this.suCommand.execRootCmd("ps | grep \"system/bin/sh\"").size() >= 6) {
            return true;
        }
        return false;
    }

    public boolean startDaemon() {
        Log.d(this.TAG, "startDaemon");
        if (isDaemonRunning()) {
            Log.w(this.TAG, "守护程序已启动");
            return false;
        }
        if (!isDaemonExisted()) {
            Log.w(this.TAG, "初始化守护程序");
            initDaemon();
        }
        return execRootCmdSilent("/system/xbin/daemon_powerkit &") == 0 ? true : true;
    }

    private int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;
        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Throwable th) {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e3) {
                    e3.printStackTrace();
                }
            }
            throw th;
        }
        return result;
    }
}
